package zm.irc;

import java.io.IOException;

import org.apache.log4j.Logger;

import zm.irc.client.IrcClient;
import zm.irc.ioc.ApplicationContext;
import zm.irc.ioc.ApplicationInitalizion;

public class RunIrcClientBackEnd {
    private static final Logger log = Logger.getLogger(RunIrcClientBackEnd.class);

    public static String dbPwd="";
    public static String baiduSecretKey="";
    public static String saslPwd="";


    /**
     * Startup : java -jar ./target/{Jar file name}.jar {irc sasl passwd} {db pwd} {baidu api secretKey}
     * Startup backend : nohup java -jar ./target/{Jar file name}.jar {db pwd} {baidu api secretKey} >> ./console.log 2>&1 &
     * @param args
     * @throws IOException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws IOException, InterruptedException {
        saslPwd = args[0];
        if(args != null && args.length >=3 ){
            dbPwd = args[1];
            baiduSecretKey = args[2];
        }
        
        IrcClient libera = initClient();
      
        libera.start();
        /**
         * Loop all channel and clean their message buffer.
         * The client will consume message from current channel's buffer.
         */
        while(true){
            try {
                Thread.sleep(50);
                libera.switchToNextChannel(); // Switch current channel one by one.
            }catch (Exception e){
                log.error("Switch channel error！",e);
            }
        }

    }

    private static IrcClient initClient() throws IOException {
        ApplicationContext appContext = ApplicationInitalizion.initApplicationContext();
        IrcClient libera = appContext.getBean(IrcClient.class);
        return libera;
    }
}


